package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SysDept;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysSchool;
import com.ruoyi.system.service.ISysSchoolService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学校Controller
 * 
 * @author ruoyi
 * @date 2025-01-06
 */
@Controller
@RequestMapping("/system/school")
public class SysSchoolController extends BaseController
{
    private String prefix = "system/school";

    @Autowired
    private ISysSchoolService sysSchoolService;

    @RequiresPermissions("system:school:view")
    @GetMapping()
    public String school()
    {
        return prefix + "/school";
    }

    /**
     * 查询学校列表
     */
    @RequiresPermissions("system:school:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysSchool sysSchool)
    {
        startPage();
        List<SysSchool> list = sysSchoolService.selectSysSchoolList(sysSchool);
        return getDataTable(list);
    }

    /**
     * 加载部门列表树
     */
    @GetMapping("/schoolTreeData")
    @ResponseBody
    public List<Ztree> schoolTreeData()
    {
        List<Ztree> ztrees = sysSchoolService.selectSchoolTree(new SysSchool());
        return ztrees;
    }

    /**
     * 选择部门树
     *
     * @param schoolId 部门ID
     * @param excludeId 排除ID
     */
        @GetMapping(value = { "/selectSchoolTree/{schoolId}", "/selectSchoolTree/{schoolId}/{excludeId}" })
    public String selectDeptTree(@PathVariable("schoolId") Long schoolId,
                                 @PathVariable(value = "excludeId", required = false) Long excludeId, ModelMap mmap)
    {
        mmap.put("school", sysSchoolService.selectSysSchoolBySchoolId(schoolId));
        mmap.put("excludeId", excludeId);
        return prefix + "/tree";
    }

    /**
     * 导出学校列表
     */
    @RequiresPermissions("system:school:export")
    @Log(title = "学校", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysSchool sysSchool)
    {
        List<SysSchool> list = sysSchoolService.selectSysSchoolList(sysSchool);
        ExcelUtil<SysSchool> util = new ExcelUtil<SysSchool>(SysSchool.class);
        return util.exportExcel(list, "学校数据");
    }

    /**
     * 新增学校
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存学校
     */
    @RequiresPermissions("system:school:add")
    @Log(title = "学校", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysSchool sysSchool)
    {
        return toAjax(sysSchoolService.insertSysSchool(sysSchool));
    }

    /**
     * 修改学校
     */
    @RequiresPermissions("system:school:edit")
    @GetMapping("/edit/{schoolId}")
    public String edit(@PathVariable("schoolId") Long schoolId, ModelMap mmap)
    {
        SysSchool sysSchool = sysSchoolService.selectSysSchoolBySchoolId(schoolId);
        mmap.put("sysSchool", sysSchool);
        return prefix + "/edit";
    }

    /**
     * 修改保存学校
     */
    @RequiresPermissions("system:school:edit")
    @Log(title = "学校", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysSchool sysSchool)
    {
        return toAjax(sysSchoolService.updateSysSchool(sysSchool));
    }

    /**
     * 删除学校
     */
    @RequiresPermissions("system:school:remove")
    @Log(title = "学校", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysSchoolService.deleteSysSchoolBySchoolIds(ids));
    }
}
